package com.realgodo.site.web.admin.constellation;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.realgodo.site.entity.dic.Constellation;
import com.realgodo.site.entity.dic.ConstellationBlood;
import com.realgodo.site.service.dic.ConstellationBloodManager;
import com.realgodo.site.service.dic.ConstellationManager;
import com.realgodo.site.web.admin.AdminDefaultController;

@Scope("prototype")
@Results( { @Result(name = "toindex", type = "redirect", location = "${base}/admin/constellation/"),
	 		@Result(name = "todetail", type = "redirect", location = "${base}/admin/constellation/index!detail?id=${id}"),
			@Result(name = "toblood", type = "redirect", location = "${base}/admin/constellation/index!blood?id=${id}")
		})
public class IndexController extends AdminDefaultController {

	/***/
	private static final long serialVersionUID = -5593473611198994991L;
	
	@Autowired
	private ConstellationManager constellationManager;
	
	@Autowired
	private ConstellationBloodManager constellationBloodManager;
	
	
	private Constellation constellation;
	
	private ConstellationBlood constellationBlood;
	
	private List<Constellation> constellationList;
	
	private List<ConstellationBlood> constellationBloodList;
	
	public String execute() throws Exception {
		constellationList=constellationManager.getConstellations(ConstellationManager.DAFAULT_LONG_NULL_VALUE);
		return SUCCESS;
	}
	
	public String detail(){
		constellation=constellationManager.get(id);
		constellationBloodList=constellationBloodManager.getConstellationBloods(id);
		return "detail";
	}
	
	public String add(){
		constellation=new Constellation();
		return "add";
	}
	
	public String edit(){
		detail();
		return "add";
	}
	
	public String delete(){
		constellationManager.deleteConstellation(id);
		return "toindex";
	}
	
	public String save(){
		constellationManager.save(constellation);
		return "toindex";
	}
	
	public String blood(){
		constellationBlood=constellationBloodManager.get(id);
		return "blood_detail";
	}
	
	public String editblood(){
		constellationBlood=constellationBloodManager.get(id);
		return "blood";
	}
	
	public String addBlood(){
		constellation=constellationManager.get(id);
		constellationBlood=new ConstellationBlood();
		constellationBlood.setConstellation(constellation);
		return "blood";
	}

	public String saveBlood(){
		constellation=constellationManager.get(id);
		constellationBlood.setConstellation(constellation);
		constellationBloodManager.save(constellationBlood);
		id=constellationBlood.getId();
		return "toblood";
	}
	
	public String deleteblood(){
		constellationBlood=constellationBloodManager.get(id);
		Long constellationId=constellationBlood.getConstellation().getId();
		constellationBloodManager.delete(id);
		id=constellationId;
		return "todetail";
	}
	
	//-------------get/set---------//
	public List<Constellation> getConstellationList() {
		if(constellationList==null){
			constellationList=new ArrayList<Constellation>();
		}
		return constellationList;
	}

	public List<ConstellationBlood> getConstellationBloodList() {
		if(constellationBloodList==null){
			constellationBloodList=new ArrayList<ConstellationBlood>();
		}
		return constellationBloodList;
	}

	public Constellation getConstellation() {
		return constellation;
	}

	public void setConstellation(Constellation constellation) {
		this.constellation = constellation;
	}

	public ConstellationBlood getConstellationBlood() {
		return constellationBlood;
	}

	public void setConstellationBlood(ConstellationBlood constellationBlood) {
		this.constellationBlood = constellationBlood;
	}

	
	
	
}
